const express = require('express')
const bodyParser = require('body-parser')
const fs = require('fs')


var comments = [
    {
        name: 'Tom1',
        message: '今天天气真不错',
        date: '2020-07-01'
    },
    {
        name: 'Tom2',
        message: '今天天气真不错',
        date: '2020-07-02'
    },
    {
        name: 'Tom3',
        message: '今天天气真不错',
        date: '2020-07-03'
    },
    {
        name: 'Tom4',
        message: '今天天气真不错',
        date: '2020-07-04'
    },
    {
        name: 'Tom5',
        message: '今天天气真不错',
        date: '2020-07-05'
    },
]

var app = express()
// engine 第一个参数 引入文件的类型（后缀名）
app.engine('html', require('express-art-template')); // 配置模板引擎
app.set('views','../public')
app.use('/public',express.static('../public'))
// body-parser 配置
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.get('/',(req,res)=>{
    // res.send('/')
    res.render('index.html',{
        comments
    })
})

app.get('/post',(req,res)=>{
    res.render('post.html')
})

app.post('/post',(req,res)=>{
    var cmt = {
        name: req.body.name,
        message: req.body.message,
        date: '2020-07-24'
    }
    comments.unshift(cmt)
    res.redirect('/')
})

app.get('/pinglun',(req,res)=>{
    var cmt = {
        name: req.query.name,
        message: req.query.message,
        date: '2020-07-24'
    }
    comments.unshift(cmt)
    res.redirect('/')
    // res.setHeader('Location','/')
    // res.statusCode = 302
    // res.end()
})

app.listen(8004)